package Controllers;

import Dao.ContactInfoMapper;
import Entity.ContactInfo;
import Util.Constant;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ContactInfoController {

    @Autowired
    private ContactInfoMapper contactInfoMapper;

    @RequestMapping(value = "/api/GetContactInfo", produces = "text/plain;charset=utf-8")
    public String getAllType() {
        List<ContactInfo> contactInfos = contactInfoMapper.getContactInfo();
        for (ContactInfo contactInfo : contactInfos) {
            if (contactInfo.getAndroidAppQrcode() != null  && !"null".equals(contactInfo.getAndroidAppQrcode())) {
                contactInfo.setAndroidAppQrcode(Constant.QRCODE_IMAGE_HEAD_ADDRESS + contactInfo.getAndroidAppQrcode());
            }
            if (contactInfo.getQqQrcodePath() != null && !"null".equals(contactInfo.getQqQrcodePath())) {
                contactInfo.setQqQrcodePath(Constant.QRCODE_IMAGE_HEAD_ADDRESS + contactInfo.getQqQrcodePath());
            }
            if (contactInfo.getWechatQrcodePath() != null && !"null".equals(contactInfo.getWechatQrcodePath())) {
                contactInfo.setWechatQrcodePath(Constant.QRCODE_IMAGE_HEAD_ADDRESS + contactInfo.getWechatQrcodePath());
            }
            if (contactInfo.getMiniProgramQrcode() != null && !"null".equals(contactInfo.getMiniProgramQrcode())) {
                contactInfo.setMiniProgramQrcode(Constant.QRCODE_IMAGE_HEAD_ADDRESS + contactInfo.getMiniProgramQrcode());
            }
        }
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(contactInfos);
        System.out.println("ContactInfo:" + json.toString());
        return json;
    }
}
